//
//  CustomTabBarController.m
//  Nurse
//
//  Created by Sunny on 13-12-16.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "CustomTabBarController.h"

#define Tabbar_H 50


@interface CustomTabBarController ()

@end

@implementation CustomTabBarController

-(id)init{
    self = [super init];
    if (self) {
        //[self setup ];
    }
    return  self;
}


-(void)setSelected:(int)index
{
    
    
    [self setSelectedIndex:index];
}

-(void)tabbarSelect:(int)index
{
    [self tabbarSelectIndex:index];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)setViewControllers:(NSArray *)viewControllers{
    [super setViewControllers:viewControllers];
    
    [self buildCustomerTabbar];
    
    [self buildBtn];
    
    [self tabbarSelectIndex:0];//修改tabbar第一个样式
    
}


-(void)buildCustomerTabbar{
    if (!_customerTabbar) {
        _customerTabbar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - Tabbar_H, self.view.width , Tabbar_H)];
        [self.view addSubview:_customerTabbar];
        //tabbar 上面蓝线
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, -1, self.view.width, 2)];
        [_customerTabbar addSubview:lbl];
        lbl.backgroundColor = MainGreenColor;
        [_customerTabbar addSubview:lbl];
        
        self.tabBarController.tabBar.hidden =YES;
        
    }
    _customerTabbar.backgroundColor = [UIColor whiteColor];
    [self.view bringSubviewToFront:_customerTabbar];
    
}

-(void)buildBtn{
    for (int i=0; i<self.viewControllers.count; i++) {
        int aBtnW = SCREEN_WIDTH/self.viewControllers.count;
        UIViewController *vc = [self.viewControllers objectAtIndex:i];
       
        UIButton *iconBtn=[[UIButton alloc]initWithFrame:CGRectMake(aBtnW * i+(aBtnW-16*1.8)/2.0-5, 0, 23*1.8, 16*1.8)];
        [iconBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"foot-icon_0%d-grey.png",i+1]] forState:UIControlStateNormal];
        [iconBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"foot-icon_0%d-active.png",i+1]] forState:UIControlStateSelected];
        iconBtn.tag = 10+i;
        iconBtn.userInteractionEnabled=NO;
        [_customerTabbar insertSubview:iconBtn atIndex:0];
        
        
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(aBtnW * i, 26, aBtnW, 20)];
        titleLab.textAlignment=NSTextAlignmentCenter;
        titleLab.font=[UIFont systemFontOfSize:12.f];
        titleLab.textColor=[UIColor blackColor];
        titleLab.tag=20+i;
        titleLab.text=vc.title;
       [_customerTabbar insertSubview:titleLab atIndex:0];
        
        
        
//        UIImage *img = vc.tabBarItem.image;
        UIButton *btn = [[UIButton alloc] init];
//        [btn setImage:img forState:UIControlStateNormal];
        btn.frame =CGRectMake(aBtnW * i, 0, aBtnW, Tabbar_H);
        [_customerTabbar insertSubview:btn atIndex:0];
        btn.tag = i+1;
//        [btn setTitle:vc.title forState:UIControlStateNormal];
//        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
       
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    }
}


-(void)btnClick:(UIButton *)sender{
    int tag = sender.tag;
    
    int index = tag - 1;
    [self setSelectedIndex:index];
    
    [self tabbarSelectIndex:index];
}

-(void)tabbarSelectIndex:(int)index{
    
    for (int i=0; i<5; i++) {
        
        if (index==i) {
            UIButton *iconBtn=(UIButton *)[_customerTabbar viewWithTag:10+i];
            [iconBtn setSelected:YES];
            UILabel *titleLab=(UILabel *)[_customerTabbar viewWithTag:20+i];
            titleLab.textColor=NavigationBarColor;
        }else{
            UIButton *iconBtn=(UIButton *)[_customerTabbar viewWithTag:10+i];
            [iconBtn setSelected:NO];
            UILabel *titleLab=(UILabel *)[_customerTabbar viewWithTag:20+i];
            titleLab.textColor=[UIColor blackColor];
        }
    }
    
}



-(void)hideTabbar{
    _customerTabbar.hidden = YES;
    self.tabBar.hidden =YES;

}

-(void)showTabbar{
    _customerTabbar.hidden = NO;
    [self.view bringSubviewToFront: _customerTabbar ];
    self.tabBar.hidden =YES;

}
@end
